﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace weiqijiance.不透光
{
     public class btgmingquan5102:btgbass
    {

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="sSet1">串口设置</param>
        /// <returns>是否成功打开</returns>
        public override bool openCOM(string sSet1)
        {
            bool breturn = base.openCOM(sSet1);
            commex.ReadTimeout = 500;
            //commex.ReceivedBytesThreshold = 46;//达到50字节以后才开始触发串口
            commex.DataReceived += comGAS_DataReceived;
            return breturn;
        }

        /// <summary>
        /// 发送串口数据
        /// </summary>
        /// <param name="cmd">通信命令
        /// 1:抽样气命令;
        /// 2:停止动作命令;
        /// 3:实时数据获取命令;
        /// 4:反吹;
        /// 5:仪器调零命令;
        /// 6:实时状态获取命令;
        /// 7:抽空气命令;
        /// 8:手动反吹命令;
        /// 9:通校准气命令;
        /// 10:通检查气命令;
        /// 11:通零气命令;
        /// 12:自动反吹命令;
        /// 13:背景空气测定命令;
        /// 14:环境空气测定命令;
        /// 15:泄漏检查命令;
        /// 16:预热期间进入系统命令;
        /// 17:背景气体数据获取命令;
        /// 18:环境气体数据获取命令;
        /// 19:按键锁止命令;
        /// 20:按键解锁命令;
        /// 21:清除烟度计最大值命令;
        /// 22:获取烟度计最大值命令;
        /// 23:烟度计调零命令;
        /// 24:气体调零命令;</param>
        /// <returns></returns>
        public override bool SendDeviceCmd(int cmd)
        {
            base.SendDeviceCmd(cmd);
            switch (cmd)
            {
                case 1:
                    {
                        sendcmd(0x10, 0xE8);
                        objzsy.SendDeviceCmd(); break;
                    }    // 1:抽样气命令;
                case 2: { sendcmd(0x16, 0xE2); break; }    // 2:停止动作命令;
                case 3:
                    {
                        SendDeviceCmdSK(); break;
                    }    // 3:实时数据获取命令;
                case 4: { sendcmd(0x17, 0xE1); break; }    // 4:反吹;
                case 5: { sendcmd(0x21, 0xD7); break; }    // 5:仪器调零命令;
                case 6: { sendcmd(0x61, 0x97); break; }    // 6:实时状态获取命令;
                case 7: { sendcmd(0x11, 0xE7); break; }    // 7:抽空气命令;
                case 8: { sendcmd(0x12, 0xE6); break; }    // 8:手动反吹命令;
                case 9: { sendcmd(0x13, 0xE5); break; }    // 9:通校准气命令;
                case 10: { sendcmd(0x14, 0xE4); break; }   // 10:通检查气命令;
                case 11: { sendcmd(0x15, 0xE3); break; }   // 11:通零气命令;
                case 12: { sendcmd(0x17, 0xE1); break; }   // 12:自动反吹命令;
                case 13: { sendcmd(0x18, 0xE0); break; }   // 13:背景空气测定命令;
                case 14: { sendcmd(0x19, 0xDF); break; }   // 14:环境空气测定命令;
                case 15: { sendcmd(0x1A, 0xDE); break; }   // 15:泄漏检查命令;
                case 16: { sendcmd(0x1F, 0xD9); break; }   // 16:预热期间进入系统命令;
                case 17: { sendcmd(0x33, 0xC5); break; }   // 17:背景气体数据获取命令;
                case 18: { sendcmd(0x34, 0xC4); break; }   // 18:环境气体数据获取命令;
                case 19: { sendcmd(0x54, 0xA4); break; }   // 19:按键锁止命令;
                case 20: { sendcmd(0x55, 0xA3); break; }   // 20:按键解锁命令;
                case 21: { sendcmd(0x63, 0x95); break; }     // 21:清除烟度计最大值命令;
                case 22: { sendcmd(0x64, 0x94); break; }     // 22:获取烟度计最大值命令;
                case 23: { sendcmd(0x2A, 0xCE); break; }     // 23:烟度计调零命令;
                case 24: { sendcmd(0x29, 0xCF); break; }     // 24:气体调零命令;
            }

            return true;
        }


        private bool SendDeviceCmdSK()
        {
            byte[] cCmdBuf = new byte[6] { 0x05, 0x30, 0x05, 0x02, 0x01, 0xC3 };
            if (commex.IsOpen)
            {
                Byte[] m_byte = new Byte[6];
                for (int i = 0; i < 6; i++)
                {
                    m_byte[i] = cCmdBuf[i];
                }
                commex.Write(m_byte, 0, 6);
            }
            return true;
        }
        private bool sendcmd(byte cCmd2, byte cCmd4)
        {
            byte[] cCmdBuf = new byte[4] { 0x00, 0x00, 0x00, 0x00 };
            cCmdBuf[0] = 0x05;
            cCmdBuf[1] = cCmd2;
            cCmdBuf[2] = 0x03;
            cCmdBuf[3] = cCmd4;//
            if (commex.IsOpen)
            {
                Byte[] m_byte = new Byte[4];
                for (int i = 0; i < 4; i++)
                {
                    m_byte[i] = cCmdBuf[i];
                }
                commex.Write(m_byte, 0, 4);
            }
            return true;
        }
        private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建，定义到外面。
        private List<byte> buffer1 = new List<byte>(4096);
        void comGAS_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            #region 5012
            if (commex.IsOpen)
            {
                System.Threading.Thread.Sleep(300);
                int n = commex.BytesToRead;
                byte[] buf = new byte[n];
                commex.Read(buf, 0, n);
                //1.缓存数据
                buffer1.AddRange(buf);
                //2.完整性判断
                if (buffer1.Count > 22)
                {
                    for (int i = 0; i < buffer1.Count; i++)
                    {
                        //2.1 查找数据头
                        if (buffer1[0 + i] == 0x06) //传输数据有帧头，用于判断
                        {
                            if (buffer1.Count - i > 22)
                            {
                                byte uSum1 = 0;
                                for (int kk = 0; kk < 22; kk++)
                                {
                                    uSum1 += (byte)(buffer1[kk + i]);
                                }
                                uSum1 = (byte)(~uSum1 + 1);
                                short uSum = 0;
                                uSum = (short)(buffer1[22 + i]);
                                if (uSum == uSum1)
                                {
                                    foreach (byte b in buffer1)
                                    {
                                        builder.Append(b.ToString("X2") + " ");
                                    }
                                    baocunriji("5102通信日志", builder.ToString());
                                    builder.Remove(0, builder.Length);//清除字符串构造器的内容  


                                    float fskls = 0;
                                    float.TryParse(string.Format("{0}", (buffer1[19 + i] * 256 + buffer1[20 + i]) * 0.01), out fskls);
                                    if (fskls < 17)
                                    {
                                        m_fsk = fskls;

                                        //float.TryParse(string.Format("{0}", (buffer1[17 + i] * 256 + buffer1[18 + i]) * 0.01), out m_fnssk);
                                        float.TryParse(string.Format("{0}", (buffer1[15 + i] * 256 + buffer1[16 + i]) * 0.01), out m_fnssk);


                                        float.TryParse(string.Format("{0}", (buffer1[11 + i] * 256 + buffer1[12 + i])), out m_fnosk);
                                        float.TryParse(string.Format("{0}", (buffer1[13 + i] * 256 + buffer1[14 + i]) * 0.01), out m_fco2sk);
                                        if (bbtgco2&&m_fco2sk < 2)
                                        {
                                            m_fco2sk = 2 + (float)(getRandom(1, 9)) * 0.1f;
                                        }
                                        float.TryParse(string.Format("{0}", (buffer1[7 + i] * 256 + buffer1[8 + i]) * 0.1), out m_fyouwenSK);
                                    }
                                    jcyq();
                                    //if (!CO2_CY.Contains("否") && m_fco2sk < 2)
                                    //{
                                    //    m_fco2sk = 2.1f + (float)(getRandom(1, 6) * 0.1) + (float)(getRandom(1, 6) * 0.01);
                                    //}
                                    //if (bxzkz)
                                    {
                                        if (m_fsk < 0.01)
                                        {
                                            //m_fsk = ((float)getRandom(1, 3)) / 100f;
                                        }
                                    }
                                    buffer1.Clear();
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        else //帧头不正确时，记得清除
                        {
                            //buffer1.RemoveAt(0);
                        }
                    }
                    //break;
                }
                else if (buffer1.Count > 10)
                {

                    if (buffer1[0] == 0x06 && buffer1[1] == 0x61 && buffer1[2] == 0x0A)
                    {
                        //if (buffer[4] == 0x80)
                        //{
                        //    m_fDLLYJ = true;
                        //}
                        if (buffer1[4] == 0x01)
                        {
                            m_fxlbs = false;
                        }
                        else if (buffer1[4] == 0x00)
                        {
                            m_fxlbs = true;
                            m_fxl = 0;
                        }
                        else if (buffer1[4] == 0x03)
                        {
                            m_fxl = 1;
                            m_fxlbs = true;
                        }
                        //m_fxl = buffer1[4];
                        //buffer.Clear();
                        //break;
                    }
                }

                buffer1.Clear();
            }
            #endregion

            //-------------------------------

        }
        public void baocunriji(string sname, string sneirong) //
        {

            string syy = DateTime.Now.ToString("yyyy");
            string smm = DateTime.Now.ToString("MM");
            string sdd = DateTime.Now.ToString("dd");
            string smul = string.Format("{0}\\{1}\\{2}\\{3}", System.Windows.Forms.Application.StartupPath, syy, smm, sdd);
            if (!Directory.Exists(smul))
            {
                Directory.CreateDirectory(smul);
            }
            string path = "";
            path = string.Format("{0}\\{1}.ini", smul, sname);

            iniclass objini = new iniclass();

            string path1 = string.Format(@"{0}", path);
            objini.Ini(path1);

            objini.Writue("日志", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), sneirong);
        }

    }
 }
